package com.skwyb.form.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skwyb.form.bean.dto.SysAdmin;
import com.skwyb.form.bean.req.IdReq;
import com.skwyb.form.bean.req.admin.GetAdminListReq;
import com.skwyb.form.bean.req.admin.SaveAdminReq;
import com.skwyb.form.core.BusinessException;
import com.skwyb.form.core.ObjectResult;
import com.skwyb.form.service.admin.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/sysAdmin")
@RequiredArgsConstructor
public class AdminController {

    private final AdminService adminService;


    @GetMapping("/list")
    public Page<SysAdmin> list(@Valid GetAdminListReq req) {
        SysAdmin record = new SysAdmin();
        BeanUtils.copyProperties(req, record);
        Page<SysAdmin> page = new Page<>(req.getPage(), req.getRows());
        return adminService.page(page, Wrappers.lambdaQuery(record));
    }

    @PostMapping("/get")
    public ObjectResult get(@Valid IdReq req) {
        return new ObjectResult(adminService.getById(req.getId()));
    }

    @PostMapping("/save")
    public ObjectResult save(@Valid SaveAdminReq req) {
        SysAdmin record = new SysAdmin();
        BeanUtils.copyProperties(req, record);
        return new ObjectResult(adminService.saveOrUpdate(record));
    }

    @PostMapping("/delete")
    public ObjectResult delete(@Valid IdReq req) throws BusinessException {
        return new ObjectResult(adminService.removeById(req.getId()));
    }
}
